package com.empresa.iflex.core;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.empresa.iflex.R;

public class Media extends Activity {
	
	public static int REQUEST_ID = 333;
	private EditText eKm, eLitros;		// Variaveis para o calculo da media de consumo
	private Float fKm, fLitros;
	private String media;
		
	/* Chamada quando o botão Menu é apertado */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu, menu);
          return true;
    }
    
    
    /* Chamad quando uma opção do Menu é selecionada */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      // Captura o menu selecionado
       switch (item.getItemId()) {
          case R.id.about:
             mAbout();
             return true;
          case R.id.sair:
             finish();
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }
    
    
    /* Opção "About" do Menu */
    private void mAbout(){  
    	Context mContext = getApplicationContext();
    	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layoutmenu = inflater.inflate(R.layout.about,
                (ViewGroup) findViewById(R.id.layoutmenu));

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);  
    	builder.setIcon(R.drawable.f);
    	builder.setTitle("iFlex");
	    builder.setView(layoutmenu)
	    	.setCancelable(false)
	    	.setPositiveButton("Ok", new DialogInterface.OnClickListener(){ 
	    		public void onClick(DialogInterface dialog, int id){  
	    			dialog.cancel();
	    	    }  
	    }); 
    	AlertDialog alert = builder.create();  
    	alert.show(); 
    	
    }
    
    
	/* Chamada quando o programa eh inicializado */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        CarregaTelaInicial();
    }
    
    
    public void CarregaTelaInicial(){
    	
    	setContentView(R.layout.media);
    	findViewById(R.id.bCalc).setOnClickListener(mResult_media);
    	eKm = (EditText) findViewById(R.id.eKm);
    	eLitros = (EditText) findViewById(R.id.eLitros);
    }
    
    
    /* Exibe o resultado da funcao de comparacao de combustiveis */
    public final Button.OnClickListener mResult_media = new Button.OnClickListener() {
        public void onClick(View v) {
        	
        	try {
				fKm = Float.parseFloat(eKm.getText().toString());
				fLitros = Float.parseFloat(eLitros.getText().toString());
				float fMedia = fKm/fLitros;
				
				DecimalFormat decimal = new DecimalFormat("0.00");
				
				media = decimal.format(fMedia);
				
				Mensagem();
			
        	} catch (NumberFormatException e) {
				// Toast pra evitar campos em branco
				Toast.makeText(getApplicationContext(), "Digite os valores", Toast.LENGTH_SHORT).show();
			}
        }
    };
    
    
    /* Mensagem de apresentação do melhor combustível */
    private void Mensagem(){  
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);  
    	builder.setIcon(R.drawable.f);
    	builder.setTitle("Consumo médio");
	    builder.setMessage("O consumo médio foi de: \n              " + media + "km/L \n\nDeseja salvar esses dados?")
	    	.setCancelable(false)
	    	.setPositiveButton("Sim", new DialogInterface.OnClickListener(){ 
	    		public void onClick(DialogInterface dialog, int id){  
	    			Intent myIntent = new Intent(getApplicationContext(), Add.class);
	    			Bundle b = new Bundle();
	    			b.putFloat("distancia", fKm);
	    			b.putFloat("consumo", fLitros);
	    			myIntent.putExtras(b);
	    			startActivity(myIntent);
	    			finish();
	    	    }  
	    });
	    builder.setNegativeButton("Não", new DialogInterface.OnClickListener(){  
  	          public void onClick(DialogInterface dialog, int id){  
  	               finish();  
  	          }  
  	     }); 
    	AlertDialog alert = builder.create();  
    	alert.show();  
    	}   
}
